<?php

namespace App\Http\Controllers\Api\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Facades\Services\User\WeChatService;

use App\Facades\Services\User\ReceiveMessageService;

use App\Facades\Utils\ValidateParameter;

class WeChatController extends Controller
{
    public function serve()
    {
        $wechat = app('wechat');

        $userApi = $wechat->user;

        $wechat->server->setMessageHandler(function($message) use ($userApi){
            if($message->MsgType == 'event'){
                switch ($message->MsgType) {
                    case 'event':
                        if($message->Event == 'subscribe'){
                            if(isset($message->EventKey)){
                                WeChatService::qRCodeSubcribe($userApi->get($message->FromUserName),$message->EventKey);
                            }
                            else{
                                WeChatService::subscribe($userApi->get($message->FromUserName));
                            }
                        }
                        if($message->Event == 'unsubscribe'){
                            WeChatService::unsubscribe($userApi->get($message->FromUserName));
                        }
                        return '欢迎关注'.$userApi->get($message->FromUserName)->nickname.'佑道健康';
                        break;
                    case 'text':
                        return '收到文字消息，用戶名:'.$userApi->get($message->FromUserName)->nickname;
                        break;
                    case 'image':
                        return '收到图片消息';
                        break;
                    case 'voice':
                        return '收到语音消息';
                        break;
                    case 'video':
                        return '收到视频消息';
                        break;
                    case 'location':
                        return '收到坐标消息';
                        break;
                    case 'link':
                        return '收到链接消息';
                        break;
                    default:
                        return '收到其它消息';
                        break;
                }
            }
        });

        return $wechat->server->serve();
    }


    public function deleteMenu(){
        $wechat = app('wechat');

        $menuApi = $wechat->menu;

        $menuApi->destroy();

        return formatJsonResponse();
    }

    public function createMenu(){
        $wechat = app('wechat');

        $menuApi = $wechat->menu;

        $buttons = [
            [
                "type" => "view",
                "name" => "首页",
                "url"  => url("index")
            ],
            [
                "type" => "view",
                "name" => "在线咨询",
                "url"  => url("online")
            ]
        ];

        $menuApi->add($buttons);
    }

    public function receive(Request $request){

        $rules = [
            'openid' => 'required|exists:wechat_users,openid',
            'message' => 'required'
        ];

        ValidateParameter::validate($request,$rules);

        $data= $request->all();

        ReceiveMessageService::receiveMessage($data);

        return formatJsonResponse();
    }
}
